﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using BaiduIntern.Model;
using System.Xml;
using System.IO.IsolatedStorage;

namespace BaiduIntern.ViewModel
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public List<InternJob> Jobs
        {
            get;
            set;
        }
        public MainViewModel()
        {

            IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

            if (!myStore.FileExists("NewData.xml"))
            {
                var sr = Application.GetResourceStream(new Uri("/BaiduIntern;component/Data.xml", UriKind.Relative));

                using (Stream fStream = sr.Stream)
                {
                    XmlSerializer xml = new XmlSerializer(typeof(List<InternJob>));
                    Jobs = (List<InternJob>)xml.Deserialize(fStream);
                }

               
            }
            else
                // Specify the file path and options.
                using (var isoFileStream = new IsolatedStorageFileStream("NewData.xml", FileMode.OpenOrCreate, myStore))
                    using (Stream fStream = isoFileStream)
                    {
                        XmlSerializer xml = new XmlSerializer(typeof(List<InternJob>));
                        Jobs = (List<InternJob>)xml.Deserialize(fStream);
                    }
                 (Application.Current as App).Jobs = this.Jobs;
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}
